//
//  bidge.m
//  hw2
//
//  Created by Pete Hobson on 06/01/2007.
//  Copyright 2007 freesome.com All rights reserved.
//

#import "bridge.h"



@implementation bridge
+ (id) makeNewObject
{
	id newObject = [[super alloc] init];
	return [newObject autorelease];
}
	
NSTimer* statusTimer;
- (void) start 
{
	//statusTimer = [[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(getCurrentStatus:) userInfo:nil repeats:YES] retain];

	discovery = [[WiiRemoteDiscovery alloc] init];
	[discovery setDelegate:self];
	[discovery retain];
	[discovery start];
	NSLog(@"Discovery started");
	sendCallback("Wiimote.discoveryStarted");


}
- (void) killMe
{
	[wii closeConnection];
	[wii release];
	wii = nil;
	[discovery release];
	discovery = nil;
} 

//delegats implementation

//discovery
- (void) WiiRemoteDiscoveryError:(int)code {
	NSLog(@"error %d", code);
	sendCallback("Wiimote.DiscoveryError:" );

	[wii closeConnection];
	[wii release];
	wii = nil;
	
	[discovery start];

}

- (void) wiiRemoteDisconnected:(IOBluetoothDevice*)device {
	sendCallback("Wiimote.disconnected");
	[wii release];
	wii = nil;
	[discovery start];
	NSLog(@"Restarting discovery!");
}


- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote {
	NSLog(@"Wiimote Discovered");
	sendCallback("Wiimote.discovered");
	wii = wiimote;
	[wiimote setDelegate:self];
	
	[wiimote setLEDEnabled1:YES enabled2:NO enabled3:NO enabled4:NO];
	[wiimote setMotionSensorEnabled:YES];
	//[wiimote setExpansionPortEnabled:YES];
	[wiimote setIRSensorEnabled:YES];
	[discovery stop];


	
	[[NSNotificationCenter defaultCenter] addObserver:self
														selector:@selector(expansionPortChanged:)
														name:@"WiiRemoteExpansionPortChangedNotification"
														object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self
														selector:@selector(batteryLow:)
														name:@"WiiRemoteBatteryLowNotification"
														object:nil];
	
	


}
- (void)batteryLow:(NSNotification *)nc{
NSLog(@"battery low");
}

- (void)expansionPortChanged:(NSNotification *)nc{

	NSLog(@"expansionPortChanged");
	
	WiiRemote* tmpWii = (WiiRemote*)[nc object];
	if (![[tmpWii address] isEqualToString:[wii address]]){
		NSLog(@"expansionPortChanged return");
		return;
	}
	/*
	if ([tmpWii isExpansionPortAttached]){
		[tmpWii setExpansionPortEnabled:YES];

	}else{
		[tmpWii setExpansionPortEnabled:NO];

	}
	*/
	if ([wii isExpansionPortAttached]){ 
		[wii setExpansionPortEnabled:YES];

	}else{
		[wii setExpansionPortEnabled:NO];

	}
	
}

-(void) acCalibration:(int)type accX_zero:(unsigned char)accX_zero accY_zero:(unsigned char)accY_zero accZ_zero:(unsigned char)accZ_zero  accX_1g:(unsigned char)accX_1g  accY_1g:(unsigned char)accY_1g  accZ_1g:(unsigned char)accZ_1g {

		sendACalibration(type, (int)accX_zero, (int)accY_zero, (int)accZ_zero,  (int)accX_1g, (int)accY_1g, (int)accZ_1g );


}
-(void) joyCalibration:(int)type x_max:(unsigned char)x_max x_min:(unsigned char)x_min x_center:(unsigned char)x_center y_max:(unsigned char)y_max y_min:(unsigned char)y_min y_center:(unsigned char)y_center {
   sendJoyCalibration(type ,(int)x_max,(int)x_min,(int)x_center,(int)y_max,(int)y_min,(int)y_center);
}

- (void) irPointMovedX:(float)px Y:(float)py{

	sendIR(px,py);
}
- (void) buttonChanged:(WiiButtonType)type isPressed:(BOOL)isPressed{

	sendBUT( (int)type, (BOOL)isPressed);

}
Boolean flip =true;
- (void) joyStickChanged:(WiiJoyStickType)type tiltX:(unsigned char)tiltX tiltY:(unsigned char)tiltY{
	//NSLog(@"joyStickChangedx %d %d", tiltX, tiltY);

	sendJS((int)type, (int)tiltX, (int)tiltY);
}

- (void) accelerationChanged:(WiiAccelerationSensorType)type accX:(unsigned char)accX accY:(unsigned char)accY accZ:(unsigned char)accZ{
			//NSLog(@"ac changed %d %d", accX, accY);
		sendAC((int)type, (int)accX, (int)accY, (int)accZ);
}

@end
